package cn.wolfcode.web.controller;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequiresPermissions(value = {"employee:list","员工列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageInfo", pageResult);
        //查询list集合
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "employee/list";
    }
    @RequiresPermissions(value = {"employee:delete","员工删除"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if(id !=null){
            employeeService.delete(id);
        }
        return new JsonResult();
    }
    @RequiresPermissions(value = {"employee:input","员工录入"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model){
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        if (id !=null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "employee/input";
    }
    @RequiresPermissions(value = {"employee:saveOrUpdate","员工保存或编辑"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        if(employee.getId() !=null){
            employeeService.update(employee,ids);
        }else{
            employeeService.save(employee,ids);
        }
        return new JsonResult();
    }
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
        return new JsonResult();
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(Long id,String name){

        HashMap<String, Boolean> map = new HashMap<>();
        //当id不为空是此时是编辑操作,查看名字是否更改,更改之后的名字不能重复
        if(id!=null){
            Employee employee = employeeService.get(id);
            if(employee.getName().equals(name)){
                map.put("valid",true);
            }else {
                employee = employeeService.selectByName(name);
                map.put("valid", employee == null);
            }
            return map;
        }else {
            Employee employee = employeeService.selectByName(name);
            map.put("valid", employee == null);
            return map;
        }
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //文件下载的响应头（让浏览器访问资源的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        HSSFWorkbook workbook = employeeService.exportXls();
        //输出到浏览器
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        employeeService.importXls(workbook);
        return new JsonResult();
    }

    @RequestMapping("/status")
    public String status(Long id) {
        Employee employee = employeeService.selectById(id);
        if(employee.isStatus()){
            employeeService.updateSelectById(employee.getId(),false);
        }else {
            employeeService.updateSelectById(employee.getId(),true);
        }
            return "redirect:/employee/list.do";
    }


}
